<template>
  <el-container class='home-container'>
    <link rel="stylesheet"
          href="./iconfont.css">
    <el-header>
      <img src="@/assets/logo.png"
           alt="系统图标">
      <span>shop-manager前端系统</span>
      <el-button type="danger"
                 @click="logout">安全退出</el-button>
    </el-header>

    <el-container>
      <el-aside :width="this.isCollapse?'60px':'150px'">
        <el-radio-group v-model="isCollapse"
                        style="margin-bottom: 20px;">
          <el-radio-button :label="false">展开</el-radio-button>
          <el-radio-button :label="true">收起</el-radio-button>
        </el-radio-group>
        <el-menu :default-active="activeItem"
                 :unique-opened="true"
                 text-color="#fff"
                 :collapse="isCollapse"
                 :router="true"
                 background-color="#373d41">
          <el-submenu :index="'/' + menu.path"
                      v-for="menu in menuList"
                      :key="menu.id">
            <template slot="title">
              <i class="el-icon-location"></i>
              <span slot="title">{{menu.authName}}</span>
            </template>
            <el-menu-item v-for="item in menu.children"
                          :key="item.id"
                          :index="'/'+item.path"
                          @click="saveActiveItem('/'+item.path)">
              <template slot="title">
                <i class="el-icon-location"></i>
                <span slot="title">{{item.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'home',
  data () {
    return {
      isCollapse: false,
      menuList: [],
      activeItem: ''
    }
  },
  async created () {
    const { data: res } = await this.$myAxios.get('/menus')
    if (res.meta.status !== 200) {
      this.$msg.error(res.meta.msg)
      return
    }
    console.log(res)
    this.menuList = res.data
    this.activeItem = window.sessionStorage.getItem('myactiveItem')
    if (!this.activeItem) {
      this.activeItem = '/users'
    }
    console.log(this.activeItem)
  },
  methods: {
    logout () {
      // window.sessionStorage.removeItem('token')
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    saveActiveItem (item) {
      window.sessionStorage.setItem('myactiveItem', item)
    }
  }
}
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > img {
    width: 60px;
    height: 60px;
  }
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 20px;
    }
  }
}
.el-aside {
  background-color: #373d41;
  .el-menu {
    .el-submenu {
      background-color: #373d41;
      .el-submenu {
        background-color: #373d41;
      }
    }
  }
}
</style>
